package util.jobs;

import java.util.List;

import ch.bfh.abch.rems.REMSMulticastController;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorController;
import controllers.SensorConfigController;
import controllers.UserConfigController;
import controllers.multicast.MessageReceiver;
import models.SensorConfig;
import models.UserConfig;
import play.Logger;
import play.db.jpa.JPABase;
import play.jobs.Job;
import play.jobs.OnApplicationStart;

@OnApplicationStart
public class ApplicationStartupJob extends Job {
    private static int DEFERRED_TIME_SECONDS = 2;

    @Override
    public void doJob() throws Exception {
        Logger.info("Do Startupjob");
        new REMSMulticastController().startMulticastReceiver(new MessageReceiver());
        new DeferredStartupJob().in(DEFERRED_TIME_SECONDS);
    }

    private class DeferredStartupJob extends Job {
        @Override
        public void doJob() throws Exception {
            Logger.info("ApplicationStartupJob");
            List<UserConfig> configs = UserConfig.findAll();
            if (configs.size() == 0) {
                Logger.info("NO Userconfig in DB, insert DefaultConfig");
                boolean foundAllSensors = false;
                while (!foundAllSensors) {
                    foundAllSensors = loadSensorConfigatStartup();
                }
                try {
                    TFSensorController sensorController = SensorConfigController.getSensorController();
                    sensorController.updateSensorList();
                    sensorController.connectToAvailableSensors();
                } catch (Exception e) {
                    Logger.error(e, "Start Datamining at Stertup failed...");
                }
            }

        }
    }

    private boolean loadSensorConfigatStartup() {
        try {
            Logger.info("Try to load SensorConfigAutomaticly");
            insertDefaultUserConfig();
        } catch (Exception e) {
            Logger.error(e, "Loead Sensorconfig at Startup failed");
            return false;
        }
        return true;
    }

    private void insertDefaultUserConfig() {
        // TODO: find a better solution NEVER copy code!!!
        UserConfig.deleteAll();
        new UserConfig(UserConfig.KEY_SENSOR_MAX_TIME_DIFF_MS, "10000", "Sekunden seit letztem Wert").save();
        new UserConfig(UserConfig.KEY_NODEAVERAGE_MAX_TIME_DIFF_MS, "15000", "Sekunden  seit letztem Wert").save();

        new UserConfig(UserConfig.TEMPERATURE_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.TEMPERATURE_IR_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.MOISTURE_MAX_DIVERGENT, "500", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.HUMIDITY_MAX_DIVERGENT, "400", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.AMBIENT_MAX_DIVERGENT, "250", "Sensorfehertoleranz").save();
        new UserConfig(UserConfig.MOTION_DETECTOR_MAX_DIVERGENT, "0", "Sensorfehertoleranz").save();

        // Maximum age of sensor values (Aktualität)
        // Maximum and minimum of effective values (Schwellwerte)
        new UserConfig(UserConfig.TEMPERATURE_MAX_VALUE_USER, "3500", "Temperatur Obergrenze").save();
        new UserConfig(UserConfig.TEMPERATURE_MIN_VALUE_USER, "170", "Temperatur Untergrenze").save();
        
        new UserConfig(UserConfig.TEMPERATURE_IR_MAX_VALUE_USER, "3500", "Temperatur Obergrenze").save();
        new UserConfig(UserConfig.TEMPERATURE_IR_MIN_VALUE_USER, "0", "Temperatur Untergrenze").save();

        new UserConfig(UserConfig.MOISTURE_MAX_VALUE_USER, "1500", "Feuchtigkeit Obergrenze").save();
        new UserConfig(UserConfig.MOISTURE_MIN_VALUE_USER, "0", "Feuchtigkeit Untergrenze").save();

        new UserConfig(UserConfig.AMBIENT_MAX_VALUE_USER, "2000", "Lux Obergrenze").save();
        new UserConfig(UserConfig.AMBIENT_MIN_VALUE_USER, "200", "Lux Untergrenze").save();

        new UserConfig(UserConfig.HUMIDITY_MAX_VALUE_USER, "535", "Luftfeuchtigkiet Obergrenze").save();
        new UserConfig(UserConfig.HUMIDITY_MIN_VALUE_USER, "100", "Luftfeuchtigkiet Untergrenze").save();
        
        new UserConfig(UserConfig.MOTION_DETECTOR_MAX_VALUE_USER, "0", "Bewegungsmelder").save();
        new UserConfig(UserConfig.MOTION_DETECTOR_MIN_VALUE_USER, "0", "Bewegungsmelder").save();
    }
}
